package com.cy.pj.sys.service.impl;

import com.cy.pj.sys.common.ServiceException;
import com.cy.pj.sys.common.annotation.RequiredLog;
import com.cy.pj.sys.dao.SysLogDao;
import com.cy.pj.sys.pojo.PageObject;
import com.cy.pj.sys.pojo.SysLog;
import com.cy.pj.sys.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SysLogServiceImpl implements SysLogService {
    @Autowired
    private SysLogDao sysLogDao;
    @Override
    public PageObject<SysLog> findPageObjects(String name, Integer pageCurrent) {
        if (pageCurrent == null || pageCurrent < 1) {
            throw new IllegalArgumentException("当前页码不正确");
        }
        int rowCount = sysLogDao.getRowCount(name);
        if (rowCount==0)
            throw new ServiceException("系统没有查到对应记录");
        int pageSize = 5;
        //获取挡前页起始位置
        int startIndex = (pageCurrent - 1) * pageSize;
        //执行数据查询
        List<SysLog> records = sysLogDao.findPageObjects(name, startIndex, pageSize);
        //封装分页信息，和当前页记录
        PageObject<SysLog> pageObject = new PageObject<>();
        pageObject.setPageCurrent(pageCurrent);
        pageObject.setPageSize(pageSize);
        pageObject.setRowCount(rowCount);
        pageObject.setRecords(records);
        pageObject.setPageCount((rowCount-1)/pageSize+1);
        return pageObject;

    }
    //日志删除
    @Override
    @RequiredLog("日志删除")
    public int deleteObjects(Integer[] ids) {
        if (ids == null || ids.length == 0) {
            throw new IllegalArgumentException("请选择一个");
        }
        int rows;
        try {
            rows = sysLogDao.deleteObjects(ids);
        } catch (Throwable throwable) {
            throw new ServiceException("系统故障，正在回复中");
        }

        if (rows == 0) {
            throw new ServiceException("记录可能已经不存在");
        }
        return rows;
    }

    @Override
    public void insertObject(SysLog sysLog) {
        sysLogDao.insertObject(sysLog);
    }
}
